using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Hector.Api;

namespace Me.Prettyprint.Cassandra.Model
{
    /// <summary>
    /// ExecutingKeyspace is directly used by the developer to execute queries.
    /// It has a handle to HConnectionManager, consistency level and failover policy.
    /// </summary>
	public class ExecutingKeyspace : Keyspace
	{
        public static Dictionary<string, string> EMPTY_CREDENTIALS = new Dictionary<string, string>();

        public ExecutingKeyspace(System.String keyspace, HConnectionManager connectionManager, ConsistencyLevelPolicy consistencyLevelPolicy, FailoverPolicy failoverPolicy)
            : this(keyspace, connectionManager, consistencyLevelPolicy, failoverPolicy, EMPTY_CREDENTIALS)
        {
        }
        public ExecutingKeyspace(string keyspace, HConnectionManager connectionManager, ConsistencyLevelPolicy consistencyLevelPolicy, FailoverPolicy failoverPolicy, Dictionary<string, string> credentials)
        {
            KeyspaceName = keyspace;
            ConnectionManager = connectionManager;
            ConsistencyLevelPolicy = consistencyLevelPolicy;
            FailoverPolicy = failoverPolicy;
            Credentials = credentials;
            ExceptionsTranslator = new ExceptionsTranslatorImpl();
        }
        virtual public ConsistencyLevelPolicy ConsistencyLevelPolicy
        {
            get;
            set;
        }
        virtual public string KeyspaceName
        {
            get;
            set;
        }
        virtual public ExceptionsTranslator ExceptionsTranslator
        {
            get;
            set;
        }
        virtual public HConnectionManager ConnectionManager
        {
            get;
            set;
        }
        virtual public FailoverPolicy FailoverPolicy
        {
            get;
            set;
        }
        virtual public Dictionary<string, string> Credentials
        {
            get;
            set;
        }
        virtual public string CqlVersion
        {
            get;
            protected set;
        }
        public long CreateClock()
        {
            return ConnectionManager.CreateClock();
        }
        public ExecutionResult<T> DoExecute<T>(KeyspaceOperationCallback<T> koc)
        {
            KeyspaceService ks = null;
            try
            {
                ks = new KeyspaceServiceImpl(KeyspaceName, ConsistencyLevelPolicy,
                    ConnectionManager, FailoverPolicy, Credentials);
                return koc.DoInKeyspaceAndMeasure(ks);
            }
            finally
            {
            }
        }
        public ExecutionResult<T> DoExecuteOperation<T>(Operation<T> operation)
        {
            operation.ApplyConnectionParams(KeyspaceName, ConsistencyLevelPolicy, FailoverPolicy, Credentials);
            ConnectionManager.operateWithFailover(operation);
            return operation.getExecutionResult();
        }
	}
}